package com.winit.common.base.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.bean.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangjian on 16/1/17.
 */
public class BaseController {

    static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    /**
     * 将用户信息拼接到返回参数中
     *
     * @param map
     * @param loginUser
     * @param data
     * @return
     */
    public Map<String, Object> getRtnMap(Map<String, Object> map, LoginUser loginUser, JSONObject data) {
        JSONObject object = new JSONObject();
        object.put("userName", loginUser.getUserName());
        object.put("userRole", loginUser.getUserRole());
        map.put("userInfo", object);
        map.put("data", data);
        return map;
    }

    /**
     * 用户登录信息不存在时返回方法
     *
     * @return
     */
    public Map<String, Object> noUserRtn() {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        rtnMap.put("code", "2");
        rtnMap.put("desc", "获取用户登录信息为空");
        return rtnMap;
    }

    @ExceptionHandler
    public Map<String, Object> exp(HttpServletRequest request, Exception ex) {
        logger.error("系统异常", ex);
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        rtnMap.put("code", "4");
        rtnMap.put("desc", "系统异常,请联系beetle管理员!" + ex.getMessage());
        return rtnMap;
    }
}
